// import { reduce } from 'core-js/core/array'
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    Details: {}, //详情页,
    loginControl: 0,
    // 购物车数据
    shopping: [
      {
        Cpmc: "健康喜乐",
        Instro: "红色康乃馨29枝、粉色百合3枝、黄色勿忘我3枝、尤加利10枝",
        ItemCode: "9012503",
        LinePrice: 390,
        Price: 292,
        Sales: "5065",
        img: "https://img01.hua.com/uploadpic/newpic/9012503.jpg_220x240.jpg",
        count: 3,
        flag:1, //判断是否选中
      }
    ],
    
    // 结算总和
    he: 0,
  },
  mutations: {
    DataClass(state, data) {
      state.Details = data
    },
    changeLoing(state, val) {
      state.loginControl = val
    },
    quitLogin(state, val) {
      state.loginControl = val
    },
    // 判断选中
    setFlag(state,ind){
      let fag = state.shopping;
      if(fag[ind].flag == 1){
        console.log('现在是未选中');
        state.shopping[ind].flag = 0;
        state.he = state.he - (state.shopping[ind].Price * state.shopping[ind].count)

      }else if(fag[ind].flag == 0){
        console.log('现在是选中');
        state.shopping[ind].flag = 1;
        state.he = state.he + (state.shopping[ind].Price * state.shopping[ind].count)

      }
    },

    // 添加购物车数据
    Addshop(state, data) {
      let index=state.shopping.findIndex(item=>item.ItemCode==data.ItemCode)
      if(index>=0){ return state.shopping[index].count++}
      data.flag = 1;
      state.shopping.push(data)
    },

    del(state, index) {
      state.shopping.splice(index, 1);
      state.he = state.shopping.map(item => item.count * item.Price).
        reduce((prev, cur) => prev + cur)
    },
    // 增减结算总和
    CountHe(state, obj) {
      state.shopping[obj.index].count = obj.count
      state.he = state.shopping.map(item => item.count * item.Price).
        reduce((prev, cur) => prev + cur)
    },
    initialize(state) {
      if (state.shopping.length) {
        state.he = state.shopping.map(item => item.count * item.Price).
          reduce((prev, cur) => prev + cur)
      }

    }
  },
  getters: {
    gets: function (state) {
      return state.shopping;
    }
  },
  actions: {

  },
  modules: {}
})




